<?php

class Sistema_Form_Produto extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        $this->setAttrib('class', 'formCtrl');
        
        $id = $front->getRequest()->getParam('id',0);
        if($id > 0){
            $obj = new Sistema_Model_DbTable_Produto();
            $data = $obj->fetchRow('PRO_ID = ' . $id)->toArray();
        }
        
        $el = new Zend_Form_Element_Hidden('PRO_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_NOM');
        $el->setLabel('Nome')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_DES');
        $el->setLabel('Descrição')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
       
        $obj = new Sistema_Model_DbTable_Catsegmento();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        $arr['0'] = 'Nenhum'; 
        foreach($row as $r) $arr[$r['CTS_ID']] = $r['CTS_NOM']; 
        
        $el = new Zend_Form_Element_Select('CTS_ID',array('onchange' => ''));
        $el->setLabel('Segmento')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        
        if($id > 0){
            $obj = new Sistema_Model_DbTable_Categoria();
            $row = $obj->fetchAll('CTS_ID ='.$data['CTS_ID'])->toArray();
            $arr = array();
            foreach($row as $r) $arr[$r['CAT_ID']] = $r['CAT_NOM']; 
        } else {
            unset($arr);
            $arr['0'] = 'Nenhum';
        }
        
        $el = new Zend_Form_Element_Select('CAT_ID',array('onchange' => ''));
        $el->setLabel('Categoria')->setRequired(true)->setRegisterInArrayValidator(false)->addMultiOptions($arr);
        $this->addElement($el);
        
        $obj = new Sistema_Model_DbTable_Fornecedor();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r) $arr[$r['FOR_ID']] = $r['FOR_NOM']; 
        
        $el = new Zend_Form_Element_Select('FOR_ID',array('onchange' => ''));
        $el->setLabel('Fornecedor')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        
        $obj = new Sistema_Model_DbTable_Gradetamanhos();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r){
            
            $seg = new Sistema_Model_DbTable_Catsegmento();
            $seg = $seg->fetchRow('CTS_ID ='.$r['CTS_ID']);
            
            $cat = new Sistema_Model_DbTable_Categoria();
            $cat = $cat->fetchRow('CAT_ID ='.$r['CAT_ID']);
            
            $arr[$r['GRA_ID']] = $seg['CTS_NOM'].' - '.$cat['CAT_NOM'].' - '.$r['GRA_SEX']; 
        }
        
        $el = new Zend_Form_Element_Select('GRA_ID',array('onchange' => ''));
        $el->setLabel('Grade de Tamanho')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        
        
        $obj = new Sistema_Model_DbTable_Gradecor();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r){
           $arr[$r['GRC_ID']] = $r['GRC_NOM']; 
        }
        
        $el = new Zend_Form_Element_Select('GRC_ID',array('onchange' => ''));
        $el->setLabel('Grade de cores')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_QTDMIN',array('onchange' => ''));
        $el->setLabel('Quantidade mínima')->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_VALPRO');
        $el->setLabel('Valor Promocional')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_DATPRO');
        $el->setLabel('Data da Promoção')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_VAL');
        $el->setLabel('Valor do Produto')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_PZO',array('onchange' => ''));
        $el->setLabel('Prazo de Entrega - (dias)')->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_PZO',array('onchange' => ''));
        $el->setLabel('Prazo de Entrega até - (dias)')->addFilter('Int');
        $this->addElement($el);
        
        
        $img = 'public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'.*';
        foreach (glob($img) as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img height="200" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element_File('PRO_FILE');
        $el->setLabel('Imagem PRINCIPAL')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        
        $img = 'public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'_2.*';
        foreach (glob($img) as $src) {
            $el = new Zend_Form_Element('img2',array( 'helper'=>'formNote', 'value'=>'<img height="200" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element_File('PRO_FILE2');
        $el->setLabel('Imagem 2')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        
        $img = 'public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'_3.*';
        foreach (glob($img) as $src) {
            $el = new Zend_Form_Element('img3',array( 'helper'=>'formNote', 'value'=>'<img height="200" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }

        $el = new Zend_Form_Element_File('PRO_FILE3');
        $el->setLabel('Imagem 3')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        $img = 'public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'_4.*';
        foreach (glob($img) as $src) {
            $el = new Zend_Form_Element('img4',array( 'helper'=>'formNote', 'value'=>'<img height="200" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element_File('PRO_FILE4');
        $el->setLabel('Imagem 4')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('PRO_STAPRO',array());
        $el->setLabel('Produto em Destaque');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('PRO_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->setLabel('Gravar');
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
    }


}

